import * as Cesium from "cesium";
function fluidPolyline(color, imgUrl, duration) {
    this._definitionChanged = new Cesium.Event();
    this._color = undefined;
    this._image = undefined;
    this._colorSubscription = undefined;
    this.color = color;
    this.image = imgUrl;
    this.duration = duration;
    this._time = new Date().getTime();
}
Object.defineProperties(fluidPolyline.prototype, {
    isConstant: {
        get: function () {
            return false;
        },
    },
    definitionChanged: {
        get: function () {
            return this._definitionChanged;
        },
    },
    color: Cesium.createPropertyDescriptor("color"),
    image: Cesium.createPropertyDescriptor("image"),
});
fluidPolyline.prototype.getType = function (time) {
    return "PolylineTrailLink";
};
fluidPolyline.prototype.getValue = function (time, result) {
    if (!Cesium.defined(result)) {
        result = {};
    }
    result.color = Cesium.Property.getValueOrClonedDefault(
        this._color,
        time,
        Cesium.Color.RED.withAlpha(0.95),
        result.color
    );
    result.image = Cesium.Property.getValueOrUndefined(this._image, time); //Cesium.Material.PolylineTrailLinkImage;
    result.time =
        ((new Date().getTime() - this._time) % this.duration) / this.duration;
    return result;
};
fluidPolyline.prototype.equals = function (other) {
    return (
        this === other ||
        (other instanceof fluidPolyline &&
            Cesium.Property.equals(this._color, other._color) &&
            Cesium.Property.equals(this._image, other._image))
    );
};
Object.fluidPolyline = fluidPolyline;
Cesium.Material.PolylineTrailLinkType = "PolylineTrailLink";
Cesium.Material.PolylineTrailLinkSource =
    "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
    {\n\
        czm_material material = czm_getDefaultMaterial(materialInput);\n\
        vec2 st = materialInput.st;\n\
        vec4 colorImage = texture(image, vec2(fract(st.s - time), st.t));\n\
        material.alpha = colorImage.a * color.a;\n\
        material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
        return material;\n\
    }";
Cesium.Material._materialCache.addMaterial(
    Cesium.Material.PolylineTrailLinkType,
    {
        fabric: {
            type: Cesium.Material.PolylineTrailLinkType,
            uniforms: {
                color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
                image: Cesium.Material.DefaultImageId,
                time: -20,
            },
            source: Cesium.Material.PolylineTrailLinkSource,
        },
        translucent: function (material) {
            return true;
        },
    }
);

function parabolaEquation(options, resultOut) {
    //方程 y=-(4h/L^2)*x^2+h h:顶点高度 L：横纵间距较大者
    var h = options.height && options.height > 5000 ? options.height : 5000;
    var L =
        Math.abs(options.pt1.lon - options.pt2.lon) >
        Math.abs(options.pt1.lat - options.pt2.lat)
            ? Math.abs(options.pt1.lon - options.pt2.lon)
            : Math.abs(options.pt1.lat - options.pt2.lat);
    var num = options.num && options.num > 50 ? options.num : 50;
    var result = [];
    var dlt = L / num;
    if (
        Math.abs(options.pt1.lon - options.pt2.lon) >
        Math.abs(options.pt1.lat - options.pt2.lat)
    ) {
        //以lon为基准
        var delLat = (options.pt2.lat - options.pt1.lat) / num;
        if (options.pt1.lon - options.pt2.lon > 0) {
            dlt = -dlt;
        }
        for (var i = 0; i < num; i++) {
            var tempH =
                h -
                (Math.pow(-0.5 * L + Math.abs(dlt) * i, 2) * 4 * h) /
                    Math.pow(L, 2);
            var lon = options.pt1.lon + dlt * i;
            var lat = options.pt1.lat + delLat * i;
            result.push([lon, lat, tempH]);
        }
    } else {
        //以lat为基准
        var delLon = (options.pt2.lon - options.pt1.lon) / num;
        if (options.pt1.lat - options.pt2.lat > 0) {
            dlt = -dlt;
        }
        for (var i = 0; i < num; i++) {
            var tempH =
                h -
                (Math.pow(-0.5 * L + Math.abs(dlt) * i, 2) * 4 * h) /
                    Math.pow(L, 2);
            var lon = options.pt1.lon + delLon * i;
            var lat = options.pt1.lat + dlt * i;
            result.push([lon, lat, tempH]);
        }
    }
    if (resultOut != undefined) {
        resultOut = result;
    }
    return result;
}

function liudongxian(viewer, color, colors1, duration, center, cities) {
    let material = new Object.fluidPolyline(
        color,
        colors1,
        duration
    );
    for (var j = 0; j < cities.length; j++) {
        var points = parabolaEquation({
            pt1: center,
            pt2: cities[j],
            height: 50000,
            num: 100,
        });
        var pointArr = [];
        for (var i = 0; i < points.length; i++) {
            pointArr.push(points[i][0], points[i][1], points[i][2]);
        }
        viewer.entities.add({
            name: "PolylineTrailLink" + j,
            polyline: {
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(pointArr),
                width: 2,
                material: material,
            },
        });
    }
}
export default { liudongxian };
